package com.wxp.entity;

import java.util.LinkedHashSet;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;

import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;
import org.hibernate.validator.constraints.Length;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.modules.model.SimpleModel;


@Entity
@Table(name = "t_unit")
public class Unit extends SimpleModel{

    @Id
    @Column(name = "UNIT_CODE_", nullable = false, insertable = true, updatable = false)
    /*@GeneratedValue(generator = "uuid")
    @GenericGenerator(name = "uuid", strategy = "com.modules.utils.IDGenerator")*/
    private String unitCode;
    
   
    
   
    @Length(max=64)
    @Column(name="UNIT_NAME_" ,nullable = true ,unique = false ,insertable = true ,updatable = true,length=64)
    private String unitName;
    
    
    @Length(max=64)
    @Column(name="LEVEL_" ,nullable = true ,unique = false ,insertable = true ,updatable = true,length=64)
    private String level;
    
    
    @Length(max=64)
    @Column(name="PARENT_CODE_" ,nullable = true ,unique = false ,insertable = true ,updatable = true,length=64)
    private String parentCode;
    
    /**
    	用来判断是新增还是已经存在的
	    1代表新增
	    2代表已经存在
     */
    
    @Length(max=64)
    @Column(name="ADD_" ,nullable = true ,unique = false ,insertable = true ,updatable = true,length=64)
    private String add;
   

    
 // 父节点
    @ManyToOne(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
    @JoinColumn(name = "PARENT_CODE_",insertable = false ,updatable = false)
    @JsonIgnore
    private Unit parent;
    
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "parent", fetch = FetchType.EAGER)
    @JsonIgnore
    private Set<Unit> children = new LinkedHashSet<Unit>();

	public String getUnitCode() {
		return unitCode;
	}


	public void setUnitCode(String unitCode) {
		this.unitCode = unitCode;
	}


	public String getUnitName() {
		return unitName;
	}


	public void setUnitName(String unitName) {
		this.unitName = unitName;
	}


	public String getLevel() {
		return level;
	}


	public void setLevel(String level) {
		this.level = level;
	}


	public Unit getParent() {
		return parent;
	}


	public void setParent(Unit parent) {
		this.parent = parent;
	}


	


	public String getParentCode() {
		return parentCode;
	}


	public void setParentCode(String parentCode) {
		this.parentCode = parentCode;
	}


	public Set<Unit> getChildren() {
		return children;
	}


	public void setChildren(Set<Unit> children) {
		this.children = children;
	}

	

	public String getAdd() {
		return add;
	}


	public void setAdd(String add) {
		this.add = add;
	}


	@Override
	public String toString() {
		return "Unit [unitCode=" + unitCode + ", unitName=" + unitName + ", level=" + level + ", parent=" + parent
				+ ", children=" + children + "]";
	}


	@Override
    public boolean equals(Object obj) {
        if(obj instanceof Unit == false) return false;
        if(this == obj) return true;
        Unit other = (Unit)obj;
        return new EqualsBuilder()
                .append(getUnitCode(),other.getUnitCode())
                .isEquals();
    }

    @Override
    public int hashCode() {
        return new HashCodeBuilder()
                .append(getUnitCode())
                .toHashCode();
    }

	
    
    
    
}